package com.highper.common.runner;

import com.highper.common.constant.ApplicationRunnerConstant;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author ZhangGJ
 * @Date 2022/01/22 15:29
 */
public class RunnerUtil {

    public static void applicationStartBanner(ConfigurableApplicationContext context,
            Environment environment) {
        double millisecond = System.currentTimeMillis() - context.getStartupDate();
        double consumeTime = millisecond / 1000;
        String applicationName =
                environment.getProperty(ApplicationRunnerConstant.SPRING_APPLICATION_NAME);
        bannerBorder(applicationName);
        String banner =
                ApplicationRunnerConstant.START_SUCCESS + formatFullTime(LocalDateTime.now(),
                        ApplicationRunnerConstant.FULL_TIME_SPLIT_PATTERN) + "\n"
                        + ApplicationRunnerConstant.APPLICATION_NAME + applicationName + "\n"
                        + ApplicationRunnerConstant.STARTUP_TIME + consumeTime + " "
                        + ApplicationRunnerConstant.SECONDS + "\n"
                        + ApplicationRunnerConstant.SERVER_PORT + environment
                        .getProperty(ApplicationRunnerConstant.L_SERVER_PORT);
        System.out.println(banner);
        bannerBorder(applicationName);
    }

    private static void bannerBorder(String applicationName) {
        for (int i = 1; i < 32 + applicationName.length(); i++) {
            System.out.print("*");
        }
        System.out.println();
    }

    private static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

}
